package com.x.provider.mc.controller.rpc;

import com.x.core.utils.BeanUtil;
import com.x.core.web.api.R;
import com.x.provider.api.mc.model.dto.*;
import com.x.provider.api.mc.service.MessageRpcService;
import com.x.provider.mc.enums.AttributeKeyGroup;
import com.x.provider.mc.service.GenericAttributeService;
import com.x.provider.mc.service.MessageEngineService;
import com.x.provider.mc.service.MessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/rpc/message")
public class MessageRpcController implements MessageRpcService {

    private final MessageService messageService;
    private final MessageEngineService messageEngineService;
    private final GenericAttributeService genericAttributeService;

    public MessageRpcController(MessageService messageService,
                                MessageEngineService messageEngineService,
                                GenericAttributeService genericAttributeService){
        this.messageService = messageService;
        this.messageEngineService = messageEngineService;
        this.genericAttributeService = genericAttributeService;
    }

    @PostMapping("send")
    @Override
    public R<MessageDTO> sendMessage(@RequestBody MessageSendReqDTO sendMessageAO) {
        return R.ok(BeanUtil.prepare(messageService.sendMessage(sendMessageAO), MessageDTO.class));
    }

    @PostMapping("send/raw")
    @Override
    public R<Long> sendMessageRaw(@RequestBody SendMessageRawDTO sendMessageAO) {
        messageEngineService.sendMessage(sendMessageAO);
        return R.ok();
    }

    @PostMapping("connect/info")
    @Override
    public R<ConnectInfoDTO> getConnectionInfo(ConnectionInfoGetReqDTO req) {
        return null;
    }

    @PostMapping("conversation/list")
    @Override
    public R<ConversationDTO> listConversation(ConversationListReqDTO req) {
        return null;
    }

    @PostMapping("conversation/get")
    @Override
    public R<ConversationDTO> getConversation(ConversationGetReqDTO req) {
        return null;
    }

    @PostMapping("list")
    @Override
    public R<MessageDTO> listMessage(MessageListReqDTO req) {
        return null;
    }

    @PostMapping("conversation/mark/as/read")
    @Override
    public R<Void> markConversationAsRead(ConversationMarkAsReadReqDTO req) {
//        messageService.markMessageAsRead();
        return R.ok();
    }

    @PostMapping("total/unread/count")
    @Override
    public R<Long> getMessageTotalUnReadCount(MessageUnreadCountTotalGetReqDTO req) {
        Long result = messageService.getTotalUnreadMessageCount(req.getCustomerId());
        return R.ok(result);
    }

    @PostMapping("resume/set")
    @Override
    public R<Void> setCustomerResume(@RequestBody AttributeSetReqDTO attributeSetDTO) {
        attributeSetDTO.setKeyGroup(AttributeKeyGroup.RESUME.name());
        genericAttributeService.addOrUpdateAttribute(attributeSetDTO);
        return R.ok();
    }
}
